package com.qy.controller.manage;

import com.github.pagehelper.PageHelper;
import com.qy.base.core.*;
import com.qy.base.utils.DateUtil;
import com.qy.model.Label;
import com.qy.service.LabelService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zaq on 2018/06/21.
 */
@RestController("Manage-LabelController")
@RequestMapping("/manage/label")
public class LabelController {
    @Resource
    private LabelService labelService;

    @PostMapping("/add")
    public Result add(@RequestBody Label label) {
        labelService.save(label);
        return ResultGenerator.successResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        labelService.deleteById(id);
        return ResultGenerator.successResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody Label label) {
        labelService.update(label);
        return ResultGenerator.successResult();
    }

    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Label label = labelService.findById(id);
        return ResultGenerator.successResult(label);
    }

    @GetMapping("/list")
    public Result list(PageBean<Label> page) {
        PageHelper.startPage(page.getPageNum(), page.getSize());
        List<Label> list = labelService.findAll();
        page.setList(list);
        return ResultGenerator.successResult(page);
    }

    //标签列表
    @GetMapping("/labelList")
    public Result labelList(Label label) {
        List<Label> list = new ArrayList<>();
        if (label.getId() == null) {
            list = labelService.selectFatherLabel();
        }else {
            list = labelService.selectChildLabel(label);
        }
        return ResultGenerator.successResult(list);
    }

    //新增标签
    @PostMapping("/addLabel")
    public Result addLabel(@RequestBody Label label) {
        if (label.getLabel_name() == null || label.getLabel_name().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        label.setAdd_time(DateUtil.getNowTimestamp());
        labelService.save(label);
        return ResultGenerator.successResult();
    }

    //编辑标签
    @PostMapping("/editLabel")
    public Result editLabel(@RequestBody Label label) {
        if (label.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        Label label1 = labelService.findById(label.getId());
        if (label.getLabel_name() == null || label.getLabel_name().isEmpty()) {
            throw new ServiceException(Constants.CODE_ERR_PARAM);
        }
        label1.setLabel_name(label.getLabel_name());
        labelService.update(label1);
        return ResultGenerator.successResult();
    }

    //删除标签
    @PostMapping("/deleteLabel")
    public Result deleteLabel(@RequestBody Label label) {
        if (label.getId() == null) {
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        labelService.deleteById(label.getId());
        return ResultGenerator.successResult();
    }

    //查询标签列表
    @GetMapping("/policyLabelList")
    public Result policyLabelList(Label label) {
        if (label.getId()==null){
            throw new ServiceException(Constants.CODE_ERR_PARAM_ID);
        }
        List<Label> list = labelService.selectChildLabel(label);
        return ResultGenerator.successResult(list);
    }
}
